/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	hey 
 * 
 * <p>描   述  :   JsRoadMatRecUtilizeServiceImpl.java for com.pengtu.gsj.service.impl.check.jsbz.materialsRecycle    </p>
 * 
 * <p>最后修改 : $: Sep 29, 2017-4:08:25 PM v 1.0.0	 hey   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jsbz.materialsRecycle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.jsbz.RoadCycleDao;
import com.pengtu.gsj.dao.system.AttachmentDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.RoadCycle;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jsbz.materialsRecycle.JsRoadMatRecUtilizeService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * JsRoadMatRecUtilizeServiceImpl
 * 
 * Sep 29, 2017 4:08:25 PM
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class JsRoadMatRecUtilizeServiceImpl implements JsRoadMatRecUtilizeService{
	
	@Autowired
	private AttachmentDao attachmentDao;
	@Autowired
	private RoadCycleDao roadCycleDao; 
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private CheckTableService checkTableService;	
	
	@Override
	@Transactional(readOnly = true)
	public List<Attachment> getAttachmentList(QueryFilter fileter) {
		return attachmentDao.find(fileter);
	}

	@Override
	public List<RoadCycle> getFieldOperations(QueryFilter queryFilter) {
		return roadCycleDao.find(queryFilter);
	}

	@Override
	public List<RoadCycle> getRoadMatRecUtilizeProject(QueryFilter queryFilter) {
		return roadCycleDao.find(queryFilter, "createDate", false);
	}

	@Override
	public Map<String, Object> getRoadMatRecUtilizes(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		List<Object> values = new ArrayList<Object>();
		List<RoadCycle> roadCyclesFirst = new ArrayList<RoadCycle>();
		List<RoadCycle> roadCyclesSecond = new ArrayList<RoadCycle>();
		List<List<String>> resultLists = new ArrayList<List<String>>();
		List<Attachment> list = new ArrayList<Attachment>();
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		String testYear = (String)jsonObject.get("thirdStepScore");
		String itemNameId = (String)jsonObject.get("itemProjectId");
		double tableFull = Double.parseDouble((String)jsonObject.get("yearFull"));  //表格总分
		double fullScoreRatio = Double.parseDouble((String)jsonObject.get("poitMark")); //可得满分的比率
		double reduceScore = Double.parseDouble((String)jsonObject.get("firstStepScore")); //每降低3个千分点扣分
		double secondStepScore = Double.parseDouble((String)jsonObject.get("secondStepScore"));
		double total = 0; 						  //总分数
		double firstScore = 0; 					 //表一得分
		double secondScore = 0; 				 //表二得分
		double percentage =0;					//系统完善度
		//根据projectId和时间、标签查询是否有附件
		list = systemManageService.getCheckAttachmentsByPartId(startTime, endTime, tag, itemNameId);
		if(StringUtils.isNotEmpty(itemNameId) || StringUtils.isNotEmpty(testYear)) {
			//如果年份不为空，进行表一的算分
			if (StringUtils.isNotEmpty(testYear)) {
				values.add(testYear);
				roadCyclesFirst = roadCycleDao.getCheckDataByTestYear(testYear);
				if (roadCyclesFirst.size() > 0){
					RoadCycle roadCycle = roadCyclesFirst.get(0);
					double wholeRate = MathsUtils.formatDouble(roadCycle.getWholeRate(), 2);
					//将所需要的值返回前端
					map.put("testYear", testYear);
					map.put("asphalMaterial", roadCycle.getAsphalMaterial());
					map.put("asphalLocalUse", roadCycle.getAsphalLocalUse());
					map.put("asphalCycleUse", roadCycle.getAsphalCycleUse());
					map.put("cementMaterial", roadCycle.getCementMaterial());
					map.put("cementLocalUse", roadCycle.getCementLocalUse());
					map.put("cementCycleUse", roadCycle.getCementCycleUse());
					map.put("asphalRate", roadCycle.getAsphalRate());
					map.put("cementRate", roadCycle.getCementRate());
					map.put("wholeRate", wholeRate);
					//根据综合循环利用率计算得分
					if (wholeRate > 0 && wholeRate < fullScoreRatio) {
						firstScore = tableFull - MathsUtils.divDouble((fullScoreRatio - roadCycle.getWholeRate()) * 10 * reduceScore, 3, 1);
						firstScore = firstScore > 0 ? firstScore : 0;
					} else if (wholeRate >= fullScoreRatio) {
						firstScore = tableFull;
					}
				}
				//添加一个标记，确定前端是否输出表格
				map.put("flag", true);
				map.put("firstScore", firstScore);
			}
		}
			//如果项目Id不为空，进行表二的算分
			if (StringUtils.isNotEmpty(itemNameId)) {
				values.add(itemNameId);
				
				String[] idArrs = itemNameId.split(",");
				for (int i = 0; i < idArrs.length; i++) {
					List<String> checkResult = Arrays.asList("0");
					RoadCycle roadCycle = roadCycleDao.get(idArrs[i]);
					roadCyclesSecond.add(roadCycle);
					if ("0".equals(roadCycle.getIsUseCase())) {
						checkResult.set(0, String.valueOf(secondStepScore));
						secondScore -= secondStepScore;
					}
					resultLists.add(checkResult);
				}
				
				map.put("flagTwo", true);
				map.put("roadCyclesSecond", roadCyclesSecond);
				map.put("resultLists", resultLists);
				
			}
			//计算两个表的总分
			total = MathsUtils.sumDouble(firstScore, secondScore);
			total = total > 0 ? total : 0;
			percentage = MathsUtils.divDouble(total * 100, tableFull, 2);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", list);
		map.put("percent", percentage);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}


	@Override
	public List<RoadCycle> getRoadMatRecUtilizeYear(String startTime, String endTime) {
		return roadCycleDao.getRoadMatRecUtilizeYear(startTime, endTime);
	}
}
